package com.ymatou.iapi.idcardmng.testcase;

import java.io.IOException;
import java.util.List;

import org.apache.http.client.ClientProtocolException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.idcardmng.parameter.IdCardStatusEnum;
import com.ymatou.iapi.idcardmng.parameter.ReceiveIdCardInfoBean;
import com.ymatou.iapi.idcardmng.parameter.SubmitIdCardInfoBean;
import com.ymatou.iapi.idcardmng.service.ReceiveIdCardInfoCall;
import com.ymatou.iapi.idcardmng.service.SubmitIdCardInfoCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P3;
import com.ymttest.database.model.YmtIdCardInfo;
import com.ymttest.database.sqlwapper.YmtIdCardInfoWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;
/**
*贝海身份证通知码头接口
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_ReceiveIdCardInfo {
	private static ReceiveIdCardInfoBean receiveidcardinfoBean;
	private static ReceiveIdCardInfoCall receiveidcardinfoCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("贝海身份证通知码头接口");
	}

	@Before
	public void caseUp() {
		receiveidcardinfoBean = new ReceiveIdCardInfoBean();
		receiveidcardinfoCall = new ReceiveIdCardInfoCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(receiveidcardinfoCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	/**
	 * 上传一个身份证信息
	 * 
	 * @return SubmitIdCardInfoBean
	 * @throws IOException 
	 * @throws ClientProtocolException 
	 */

	private SubmitIdCardInfoBean submitidOneCardinfo(
			SubmitIdCardInfoBean submitidcardinfoBean)
			throws ClientProtocolException, IOException {

		SubmitIdCardInfoCall submitidcardinfoCall = new SubmitIdCardInfoCall();

		if (submitidcardinfoBean == null) {

			submitidcardinfoBean = new SubmitIdCardInfoBean();

			submitidcardinfoBean.setBuyerId(Long.valueOf(TestDataManager
					.getRandomIntNum(6)));
			submitidcardinfoBean.setCardNumber(TestDataManager
					.getCardId(20, 10));
			submitidcardinfoBean.setName(TestDataManager.getChineseName());

		}

		submitidcardinfoBean.setCardType(1);
		submitidcardinfoBean.setHasPic(true);
		submitidcardinfoBean.setMobile("136" + TestDataManager.getRandomNum(8));
		submitidcardinfoBean.setOperater("autotest");
		submitidcardinfoBean
				.setReverseSide("http://file.iapi.ymatou.com/G01/M00/3A/76/rBBlD1ncd-GAEA_gAAB47-RPlbM047_586_367_o.jpg");
		submitidcardinfoBean
				.setRightSide("http://file.iapi.ymatou.com/G01/M00/3A/76/rBBlD1ncd7eAKwtqAABQMz0rvxE036_5_3_o.jpg");
		submitidcardinfoBean
				.setMarkRightSide("http://file.iapi.ymatou.com/G01/M00/3A/76/rBBlD1ncd7iAJ2HAAAB3SVFakLc798_5_3_o.JPG");

		submitidcardinfoBean
				.setMarkReverseSide("http://file.iapi.ymatou.com/G01/M00/3A/76/rBBlD1ncd-GAa4tdAACybz9T5zY172_586_367_o.JPG");

		submitidcardinfoBean.setCheckIdCardStatus(200);

		submitidcardinfoCall.setData(false, submitidcardinfoBean);
		submitidcardinfoCall.callService();

		return submitidcardinfoBean;

	}
	private void verifyDb() throws InterruptedException {

		Thread.sleep(1000);

		YmtIdCardInfoWapper ymtIdCard = new YmtIdCardInfoWapper();;

		YmtIdCardInfo ymtIdCardInfo = ymtIdCard.selectByNameAndId(
				receiveidcardinfoBean.getName(),
				receiveidcardinfoBean.getIdCode());

		Logger.comment("校验身份证信息表ymt_idcardinfo");

		Logger.verifyEquals(receiveidcardinfoBean.getName(),
				ymtIdCardInfo.getName(), "校验name");
		Logger.verifyEquals(receiveidcardinfoBean.getIdCode(),
				ymtIdCardInfo.getCardNumber(), "校验CardNumber");

		//贝海状态（1. 通过审核，或未开始审核， 2. 审核不通过）
		int checkStatus = receiveidcardinfoBean.getStatus() > 1
				? IdCardStatusEnum.INVALID.getCode()
				: IdCardStatusEnum.AUDIT_PASSED.getCode();

		//0通过，其它2.3.4.5.6.7.8为审核不通过状态
		int rejectReason = receiveidcardinfoBean.getStatus() > 1
				? receiveidcardinfoBean.getStatus()
				: 0;

		Logger.verifyEquals(checkStatus, ymtIdCardInfo.getStatus(), "校验Status");

		Logger.verifyEquals(receiveidcardinfoBean.isHasIdPic(),
				ymtIdCardInfo.getHasPic(), "校验HasPic");

		/*		Logger.verifyEquals(receiveidcardinfoBean.getPhone(),
						ymtIdCardInfo.getMobile(), "校验Mobile");*/

		Logger.verifyEquals(rejectReason, ymtIdCardInfo.getRejectReason(),
				"校验RejectReason");
		Logger.verifyEquals(0, ymtIdCardInfo.getIsDelete(), "校验IsDelete");

	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ReceiveIdCardInfo_001() {
		Logger.start(true, "贝海通知过来，本地如果没有数据记录，身份证服务不做任何处理");
		try {

			receiveidcardinfoBean.setName(TestDataManager.getChineseName());
			receiveidcardinfoBean.setIdCode(TestDataManager.getCardId(20, 10));
			receiveidcardinfoBean.setStatus(1);
			receiveidcardinfoBean.setHasIdPic(true);
			receiveidcardinfoBean.setPhone("136"
					+ TestDataManager.getRandomNum(8));
			receiveidcardinfoBean.setReverseSide("");
			receiveidcardinfoBean.setRightSide("");
			receiveidcardinfoBean.setBillCode(TestDataManager.getRandomNum(8));
			receiveidcardinfoBean
					.setOrderNo(TestDataManager.getRandomIntNum(8));

			receiveidcardinfoCall.setData(receiveidcardinfoBean);
			receiveidcardinfoCall.callService();

			Logger.verifyEquals("ok", receiveidcardinfoCall.getReturnData(),
					"验证接口返回结果");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ReceiveIdCardInfo_002() {
		Logger.start(true, "本地身份证记录存在，贝海身份证通知码头成功,本地更新记录成功");
		try {

			long buyerId = Long.valueOf(EnvSetup.getData("SellerId2"));

			YmtIdCardInfoWapper ymtIdCard = new YmtIdCardInfoWapper();;

			YmtIdCardInfo ymtIdCardInfo = ymtIdCard.selectByBuyerId(buyerId);

			String name = ymtIdCardInfo.getName();
			String idCode = ymtIdCardInfo.getCardNumber();

			boolean hasPic = ymtIdCardInfo.getHasPic();
			int status = ymtIdCardInfo.getStatus();

			receiveidcardinfoBean.setName(name);
			receiveidcardinfoBean.setIdCode(idCode);
			receiveidcardinfoBean.setStatus(status + 1);
			receiveidcardinfoBean.setHasIdPic(!hasPic);
			receiveidcardinfoBean.setPhone("136"
					+ TestDataManager.getRandomNum(8));
			receiveidcardinfoBean.setReverseSide("");
			receiveidcardinfoBean.setRightSide("");
			receiveidcardinfoBean.setBillCode(TestDataManager.getRandomNum(8));
			receiveidcardinfoBean
					.setOrderNo(TestDataManager.getRandomIntNum(8));

			receiveidcardinfoCall.setData(receiveidcardinfoBean);
			receiveidcardinfoCall.callService();

			Logger.verifyEquals("ok", receiveidcardinfoCall.getReturnData(),
					"验证接口返回结果");

			verifyDb();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ReceiveIdCardInfo_003() {
		Logger.start(true, "本地身份证记录存在，身份证状态为审核不通过 2,本地库更新成功并发送mq消息通知消息路由");
		try {

			long buyerId = Long.valueOf(EnvSetup.getData("SellerId2"));

			YmtIdCardInfoWapper ymtIdCard = new YmtIdCardInfoWapper();;

			YmtIdCardInfo ymtIdCardInfo = ymtIdCard.selectByBuyerId(buyerId);

			String name = ymtIdCardInfo.getName();
			String idCode = ymtIdCardInfo.getCardNumber();

			boolean hasPic = ymtIdCardInfo.getHasPic();
			int status = ymtIdCardInfo.getStatus();

			receiveidcardinfoBean.setName(name);
			receiveidcardinfoBean.setIdCode(idCode);

			receiveidcardinfoBean.setStatus(2);

			receiveidcardinfoBean.setHasIdPic(!hasPic);
			receiveidcardinfoBean.setPhone("136"
					+ TestDataManager.getRandomNum(8));
			receiveidcardinfoBean.setReverseSide("");
			receiveidcardinfoBean.setRightSide("");
			receiveidcardinfoBean.setBillCode(TestDataManager.getRandomNum(8));
			receiveidcardinfoBean
					.setOrderNo(TestDataManager.getRandomIntNum(8));

			receiveidcardinfoCall.setData(receiveidcardinfoBean);
			receiveidcardinfoCall.callService();

			Logger.verifyEquals("ok", receiveidcardinfoCall.getReturnData(),
					"验证接口返回结果");

			verifyDb();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_ReceiveIdCardInfo_004() {
		Logger.start(true, "贝海身份证状态 checkStatus为0");
		try {

			long buyerId = Long.valueOf(EnvSetup.getData("SellerId2"));

			YmtIdCardInfoWapper ymtIdCard = new YmtIdCardInfoWapper();;

			YmtIdCardInfo ymtIdCardInfo = ymtIdCard.selectByBuyerId(buyerId);

			String name = ymtIdCardInfo.getName();
			String idCode = ymtIdCardInfo.getCardNumber();

			boolean hasPic = ymtIdCardInfo.getHasPic();
			int status = ymtIdCardInfo.getStatus();

			receiveidcardinfoBean.setName(name);
			receiveidcardinfoBean.setIdCode(idCode);

			receiveidcardinfoBean.setStatus(0);

			receiveidcardinfoBean.setHasIdPic(!hasPic);
			receiveidcardinfoBean.setPhone("136"
					+ TestDataManager.getRandomNum(8));
			receiveidcardinfoBean.setReverseSide("");
			receiveidcardinfoBean.setRightSide("");
			receiveidcardinfoBean.setBillCode(TestDataManager.getRandomNum(8));
			receiveidcardinfoBean
					.setOrderNo(TestDataManager.getRandomIntNum(8));

			receiveidcardinfoCall.setData(receiveidcardinfoBean);
			receiveidcardinfoCall.callService();

			Logger.verifyEquals("ok", receiveidcardinfoCall.getReturnData(),
					"验证接口返回结果");

			verifyDb();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_ReceiveIdCardInfo_005() {
		Logger.start(true, "name为null");
		try {

			//receiveidcardinfoBean.setName(name);
			receiveidcardinfoBean.setIdCode("1111111111");

			receiveidcardinfoBean.setStatus(0);

			receiveidcardinfoBean.setHasIdPic(false);
			receiveidcardinfoBean.setPhone("136"
					+ TestDataManager.getRandomNum(8));
			receiveidcardinfoBean.setReverseSide("");
			receiveidcardinfoBean.setRightSide("");
			receiveidcardinfoBean.setBillCode(TestDataManager.getRandomNum(8));
			receiveidcardinfoBean
					.setOrderNo(TestDataManager.getRandomIntNum(8));

			receiveidcardinfoCall.setData(receiveidcardinfoBean);
			receiveidcardinfoCall.callService();

			Logger.verifyEquals("ok", receiveidcardinfoCall.getReturnData(),
					"验证接口返回结果");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_ReceiveIdCardInfo_006() {
		Logger.start(true, "IdCode为null");
		try {

			receiveidcardinfoBean.setName("zhangsan");
			//receiveidcardinfoBean.setIdCode("1111111111");

			receiveidcardinfoBean.setStatus(0);

			receiveidcardinfoBean.setHasIdPic(false);
			receiveidcardinfoBean.setPhone("136"
					+ TestDataManager.getRandomNum(8));
			receiveidcardinfoBean.setReverseSide("");
			receiveidcardinfoBean.setRightSide("");
			receiveidcardinfoBean.setBillCode(TestDataManager.getRandomNum(8));
			receiveidcardinfoBean
					.setOrderNo(TestDataManager.getRandomIntNum(8));

			receiveidcardinfoCall.setData(receiveidcardinfoBean);
			receiveidcardinfoCall.callService();

			Logger.verifyEquals("ok", receiveidcardinfoCall.getReturnData(),
					"验证接口返回结果");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_ReceiveIdCardInfo_007() {
		Logger.start(true, "phone为null");
		try {

			receiveidcardinfoBean.setName("zhangsan");
			receiveidcardinfoBean.setIdCode("1111111111");

			receiveidcardinfoBean.setStatus(0);

			receiveidcardinfoBean.setHasIdPic(false);
			/*			receiveidcardinfoBean.setPhone("136"
								+ TestDataManager.getRandomNum(8));*/
			receiveidcardinfoBean.setReverseSide("");
			receiveidcardinfoBean.setRightSide("");
			receiveidcardinfoBean.setBillCode(TestDataManager.getRandomNum(8));
			receiveidcardinfoBean
					.setOrderNo(TestDataManager.getRandomIntNum(8));

			receiveidcardinfoCall.setData(receiveidcardinfoBean);
			receiveidcardinfoCall.callService();

			Logger.verifyEquals("ok", receiveidcardinfoCall.getReturnData(),
					"验证接口返回结果");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	//上传身份证信息后，贝海也会退消息过来，case 有几率不过。需关掉贝海推送消息
	public void Tc_ReceiveIdCardInfo_008() {
		Logger.start(true, "本地身份证记录存在，贝海身份证通知码头审核不通过,本地更新改号码下所有身份证信息为审核不通过");
		try {

			SubmitIdCardInfoBean submitIdCardInfoBean = submitidOneCardinfo(null);

			String idCode = submitIdCardInfoBean.getCardNumber();
			//身份证号码相同，姓名不通的记录2 
			submitIdCardInfoBean.setName(TestDataManager.getChineseName());
			submitidOneCardinfo(submitIdCardInfoBean);
			//身份证号码相同，姓名不通的记录3
			submitIdCardInfoBean.setName(TestDataManager.getChineseName());
			submitidOneCardinfo(submitIdCardInfoBean);
			//身份证号码相同，姓名不通的记录4
			submitIdCardInfoBean.setName(TestDataManager.getChineseName());
			submitidOneCardinfo(submitIdCardInfoBean);

			receiveidcardinfoBean.setName("张武");
			receiveidcardinfoBean.setIdCode(idCode);
			receiveidcardinfoBean.setStatus(0);
			receiveidcardinfoBean.setHasIdPic(false);
			receiveidcardinfoBean.setPhone("136"
					+ TestDataManager.getRandomNum(8));
			receiveidcardinfoBean.setReverseSide("");
			receiveidcardinfoBean.setRightSide("");
			receiveidcardinfoBean.setBillCode(TestDataManager.getRandomNum(8));
			receiveidcardinfoBean
					.setOrderNo(TestDataManager.getRandomIntNum(8));

			receiveidcardinfoCall.setData(receiveidcardinfoBean);
			receiveidcardinfoCall.callService();

			Logger.verifyEquals("ok", receiveidcardinfoCall.getReturnData(),
					"验证接口返回结果");

			Thread.sleep(3000);

			YmtIdCardInfoWapper ymtIdCard = new YmtIdCardInfoWapper();;

			List<YmtIdCardInfo> list = ymtIdCard.selectByCardId(idCode);

			for (YmtIdCardInfo ymtIdCardInfo : list) {

				Logger.comment(String.format("校验姓名为%s身份证审核状态",
						ymtIdCardInfo.getName()));

				Logger.verifyEquals(-1, ymtIdCardInfo.getStatus(), "校验Status");
			}

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}